package tt.com.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import tt.com.business.mapper.ServeProvidersSubsidyMapper;
import tt.com.business.service.ServeProvidersSubsidyService;
import tt.com.common.model.Dead;
import tt.com.common.model.ServeProvidersSubsidy;
import tt.com.common.util.PageUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ServeProvidersSubsidyServicelmpl extends ServiceImpl<ServeProvidersSubsidyMapper, ServeProvidersSubsidy> implements ServeProvidersSubsidyService {
    @Override
    public ResponseEntity selectServeProvider(PageUtil pageUtil) {
        System.out.println(pageUtil);
        IPage<ServeProvidersSubsidy> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        QueryWrapper<ServeProvidersSubsidy> qw=new QueryWrapper<>();

        qw.eq("data_status",1);
        IPage<ServeProvidersSubsidy> page1 = baseMapper.selectPage(page, qw);
        pageUtil.setData(page1);
        //设计总数
        QueryWrapper<ServeProvidersSubsidy> qwe=new QueryWrapper<>();
        qw.eq("data_status",1);
        pageUtil.setTotalSize(baseMapper.selectCount(qwe));
        //设置总页数
        pageUtil.setTotalPage(pageUtil.getPageSize()/pageUtil.getPageSize());
        return new ResponseEntity(pageUtil, HttpStatus.OK);

    }

    @Override
    public ResponseEntity addServeProvider(ServeProvidersSubsidy serveProvidersSubsidy) {

        //判断该项目是否已经有惠民规则
        String projectId = serveProvidersSubsidy.getProjectId();
        QueryWrapper<ServeProvidersSubsidy > objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("project_id",projectId);
        List<ServeProvidersSubsidy> serveProvidersSubsidies = baseMapper.selectList(objectQueryWrapper);
        if (serveProvidersSubsidies.size()>0){
            return new ResponseEntity("该项目已经被注册过了",HttpStatus.INTERNAL_SERVER_ERROR);
        }
        //查询总数
        Integer integer = baseMapper.selectCount(null);
        //设置主键
        serveProvidersSubsidy.setId(String.valueOf(integer+1));
        //设置添加时间
        Date dead=new Date();
        serveProvidersSubsidy.setCreateTime(dead);
        //设置惠民规则状态
        serveProvidersSubsidy.setDataStatus(1);

        //添加惠民规则
        int insert = baseMapper.insert(serveProvidersSubsidy);

        if (insert<0){
            return new ResponseEntity("添加失败",HttpStatus.INTERNAL_SERVER_ERROR);
        }

        return new  ResponseEntity("添加成功",HttpStatus.OK);
    }
}
